<!-- 关联公司 -->
<template>
	<view class="">
		<u-sticky offset-top="0">
			<common-search
				class="search"
				v-model="queryParam.projectName" 
				:screenShow="screenShow"
				@screenclick="screenclick"
				@searchMsg="searchMsg"
				placeholder="请输入项目名"
			></common-search>
		</u-sticky>
		<view class="page" :style="!isData?'display:flex;width: 100vw;align-items:center;justify-content: center;':''">
			<template v-if="isData">
				<view class="list" v-for="(item,index) in dataList" :key="index" @click="onChecker(item, index)">
					<view class="">
						<view class="option">
							<view class="lf">项目名称</view>
							<view class="rl">{{ item.projectName }}</view>
						</view>
						<view class="option">
							<view class="lf">签订单位</view>
							<view class="rl">{{ item.signUnit_dictText }}</view>
						</view>
						<view class="option">
							<view class="lf">经营团队</view>
							<view class="rl">{{ item.teamName_dictText }}</view>
						</view>
						<view class="option">
							<view class="lf">项目归属年度</view>
							<view class="rl">{{ item.year_dictText }}</view>
						</view>
						<view class="option">
							<view class="lf">业主单位</view>
							<view class="rl">{{ item.ownerUnit_dictText }}</view>
						</view>
						<view class="option">
							<view class="lf">联系人</view>
							<view class="rl">{{ item.contacts_dictText }}</view>
						</view>
						<view class="option">
							<view class="lf">联系方式</view>
							<view class="rl">{{ item.contactTel }}</view>
						</view>
						<view class="option">
							<view class="lf">项目承接人</view>
							<view class="rl">{{ item.undertaker_dictText }}</view>
						</view>
						<view class="option">
							<view class="lf">所在地址</view>
							<view class="rl">{{ getAddress(item.areaCode) }}</view>
						</view>
						<view class="option">
							<view class="lf">项目阶段</view>
							<view class="rl">{{ item.status_dictText }}</view>
						</view>
						
					</view>
					<view class="icon" v-show="item.isChecker"><u-icon name="checkbox-mark" color="#2979ff" size="22"></u-icon></view>
				</view>
				<!-- 加载更多显示 -->
				<view class=""><u-loadmore :status="statusLoadmore" /></view>
			</template>
			<view v-else>
				<u-empty
					mode="data"
					icon="/static/empty/data.png"
				>
				</u-empty>
			</view>
			<view style="height: 100rpx;"></view>
			<view class="btn">
				<u-button text="确定" type="primary" @click="onConfirm"></u-button>
			</view>
		</view>
		
		<u-toast ref="uToast"></u-toast>
	</view>
</template>
<script>
	import { queryOmcPrepareProjectList, queryOmcProjectList } from '@/api/index.js';
	import CommonSearch from '../../components/CommonSearch.vue'
	import address from '@/utils/address.js'
	export default {
		components: {
			CommonSearch
		},
		data() {
			return {
				address,
				signUnit: '', // 收款单位id
				teamName2: '', // 经营团队id
				isBidding: '',
				projectStatus: '',
				dataIsolation: '',
				costType: '',
				
				showNotify: true, // 消息提示
				id: '', // 选中的id
				page: {
					pageNo: 1,
					pageSize: 10,
				},
				dataList: [] ,// 列表
				isData: true, // 判断是否有数据  true 代表有 false 代表无
				statusLoadmore: 'loadmore', // 通过status设置组件的状态，加载前值为loadmore，加载中为loading，没有数据为nomore
				oldIndex: 0, // 存储列表之前被选中的数据
				screenShow: false, // 筛选弹窗
				isQueryOmcPrepareProjectList: 'false',
				// 传的参数
				queryParam: {
					projectName: ''
				},
				
				objectField: '', // 点击确认需要修改的字段
				changeStatus: '',
				dataType: '',
				isOldSystem:'', // 老系统数据查询
				currentProjectStatus: ''
			}
		},
		onLoad(options) {
			this.id = options.id || ''
			this.objectField = options.objectField || ''
			this.signUnit = options.signUnit || '' // 签订单位
			this.isBidding = options.isBidding || '' // 经营团队
			this.projectStatus = options.projectStatus || '' // 经营团队
			this.teamName2 = options.teamName2 || '' // 经营团队
			this.tenderStatus = options.tenderStatus || '' // 经营团队
			this.dataIsolation = options.dataIsolation || '' // 经营团队
			this.costType = options.costType || '' // 经营团队
			this.changeStatus = options.changeStatus || '' // 经营团队
			this.dataType = options.dataType || '' // 经营团队
			this.isQueryOmcPrepareProjectList = options.queryOmcPrepareProjectList || 'false' // 使用哪个接口
			this.isOldSystem = options.isOldSystem || '' // 老系统数据查询
			this.currentProjectStatus = options.currentProjectStatus || '' // 老系统数据查询
			this.geterpOmcTeamManagerList() // 获取列表
		},
		methods: {
			// 获取列表
			geterpOmcTeamManagerList () {
				this.statusLoadmore = 'loading' // 加载中
				var params = {
					pageNo: this.page.pageNo,
					pageSize: this.page.pageSize,
					// status: 2
					
				}
				if (this.queryParam.projectName) {
					params.projectName = '*' + this.queryParam.projectName + '*'
				}
				if (this.signUnit) {
					// params.signUnit = this.signUnit
					params.companyId = this.signUnit
				}
				if (this.teamName2) {
					params.teamName2 = this.teamName2
				}
				if (this.isBidding) {
					params.isBidding = this.isBidding
				}
				if (this.projectStatus) {
					params.status = this.projectStatus
				}
				if (this.tenderStatus) {
					params.projectTenderStatus = this.tenderStatus
				}
				if (this.dataType) {
					params.dataType = this.dataType
				}
				if (this.changeStatus) {
					params.changeStatus = this.changeStatus
				}
				if (this.isOldSystem) {
					params.isOldSystem = this.isOldSystem
				}
				if (this.currentProjectStatus) {
					params.currentProjectStatus = this.currentProjectStatus
				}
				// if (this.dataIsolation) {
				// 	params.dataIsolation = this.dataIsolation
				// }
				params.dataIsolation = this.dataIsolation || ''
				params.sysOrgCode = ''
				if (this.costType) {
					params.costType = this.costType
				}
				
				var url = ''
				if (this.isQueryOmcPrepareProjectList == 'true') {
					url = queryOmcPrepareProjectList
				} else {
					if (this.projectStatus == '1') {
						url = queryOmcPrepareProjectList
					} else {
						url = queryOmcProjectList
					}
				}
				
				url(params).then(res => {
					if (res.success) {
						 // 判断首次加载是否有数据
						if (this.page.pageNo == 1){
							if (res.result.records&&res.result.records.length) {
								this.isData = true
							} else {
								this.isData = false
							}
						}
						if (res.result.records&&res.result.records.length){
							if (res.result.records.length >= 10) {
								this.statusLoadmore = 'loadmore'
							} else {
								this.statusLoadmore = 'nomore'
							}
							
							// 处理数据
							this.oldIndex = 0
							res.result.records.map((value, index) => {
								// 是否选中数据
								if (this.id == value.id) {
									value.isChecker = true
									// this.oldIndex = index // 当单选的时候
								} else {
									value.isChecker = false
								}
								return value
							})
						}else {
							this.statusLoadmore = 'nomore'
						}
						if (this.page.pageNo == 1){
							this.dataList = res.result.records
						} else {
							this.dataList = this.dataList.concat(res.result.records)
						}
						
						if (this.dataList && this.dataList.length) {
							const oldIndex =	this.dataList.findIndex(item => item.isChecker)
							if (oldIndex == -1) {
								this.oldIndex = 0
							} else {
								this.oldIndex = oldIndex
							}
						}
					}
				})
			},
			
			// 地址显示
			getAddress(areaCode) {
				var arr = areaCode.split(',')
				var addressDetail = ''
				this.address.map(value => {
					if (arr[0] && value.code == arr[0]) {
						addressDetail = value.name
						value.children.map(val => {
							if (arr[1] && val.code == arr[1]) {
								addressDetail = addressDetail + val.name
								val.children.map(item => {
									if (arr[2] && item.code == arr[2]) {
										addressDetail = addressDetail + item.name
									}
								})
							}
						})
					}
				})
				return addressDetail
			},
			
			// 点击筛选
			screenclick () {
				this.screenShow = !this.screenShow
			},
			// 输入内容
			searchMsg () {
				this.page.pageNo = 1 // 页数归1
				this.dataList = [] // 清空列表
				this.geterpOmcTeamManagerList() // 获取列表
			},
			
			// 点击选中取消选中
			onChecker(item, index) {
				// this.dataList[index].isChecker = !this.dataList[index].isChecker // 多选
				// 单选
				// 判断是否已经被选中了
				if (item.isChecker) {
					// 当已经被选中了
					this.dataList[index].isChecker = !this.dataList[index].isChecker
				} else {
					// 当没有被选中
					
					// 修改之前被选中的状态
					if (this.dataList[this.oldIndex].isChecker) {
						this.dataList[this.oldIndex].isChecker = false
					}
					// 修改现在的状态
					this.dataList[index].isChecker = true // 变成选中的状态
					this.oldIndex = index
				}
			},
			
			// 点击确定
			onConfirm () {
				var item = ''
				this.dataList.map(value => {
					if (value.isChecker) {
						item = value
					} else {
						// error
					}
				})
				if (item) {
					let arr = this.objectField.split('.')
					let pages = getCurrentPages() //获取所有页面栈实例列表
					let nowPage = pages[pages.length - 1] //当前页页面实例
					let prevPage = pages[pages.length - 2] //上一页页面实例
					if (prevPage.$vm) {
						// 多层级设置值
						const setVal = (data = {}, key, val) => {
							const keyArr = key.split('.')
							const lastKey = keyArr[keyArr.length - 1]
							keyArr.reduce((o, k) => {
								if (k === lastKey) {
									this.$set(o, k, val)
								}
								return o[k]
							}, data)
						}
						setVal(prevPage.$vm, this.objectField, item.id)
						// if (arr.length == 1) {
						// 	prevPage.$vm[this.objectField] = item.id //修改上一页data里面的tagIndex 参数值
						// } else if (arr.length == 2) {
						// 	prevPage.$vm[arr[0]][arr[1]] = item.id //修改上一页data里面的tagIndex 参数值
						// }
						
					}
					uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
					})
				} else {
					this.$refs.uToast.show({
						type: 'error',
						icon: 'none',
						message: '请选择数据'
					})
				}
			}
			
			
		},
		onReachBottom() {
			if (this.statusLoadmore == 'nomore') return false
			this.page.pageNo = ++ this.page.pageNo;
			this.geterpOmcTeamManagerList()
		}
		
	}
</script>
<style lang="scss" scoped>
.page {
	padding: 0 20rpx 20rpx 20rpx;
	.list{
		border-radius: 10rpx;
		box-shadow: 0px 4px 20px 0px rgba(0,0,0,0.1);
		padding: 12rpx 20rpx 20rpx 20rpx;
		background: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-top: 20rpx;
	}
}

// 按钮
.btn{
	position: fixed;
	bottom: 20rpx;
	width: 95%;
}
</style>
